/*
 * MeWebStudio Solutions
 * @author      : Muharrem ERİN / MeWebStudio
 * @contact     : me@mewebstudio.com
 * @copyright   : (c) 2013 MeWebStudio All rights reserved!
 *
 */

!function ($) {

	var $window = $(window);

	/* Begin: External URLs */
	$('a.ext').click(function(){
		window.open($(this).attr('href'));
		return false;
	});
	/* End: External URLs */

	$('#wrap .content').css({ minHeight: $(document).height() / 1.8 + 'px' });
	$(window).resize(function(){
		$('#wrap .content').css({ minHeight: $(document).height() / 1.8 + 'px' });
	});

	/* Begin: Header-Right Navigation */
	$('#nav-top li').each(function(){
		$(this).find('ul').css({ minWidth: $(this).width() - 22 + 'px' });
	});
	$('#nav-top li').click(function(){
		var $subnav = $(this).children('ul');
		if ($('#nav-top li ul').is(':visible'))
		{
			$('#nav-top li a').removeClass('active');
			$('#nav-top li ul').slideUp(100, 'swing');
		}
		$('#nav-top li ul a').removeClass('active');
		if ($subnav.html())
		{
			var $parentlink = $subnav.parent('li').find('a:first-child');
			if ($subnav.is(':visible'))
			{
				$parentlink.removeClass('active');
				$subnav.slideUp(100, 'swing');
			}
			else
			{
				$subnav.slideDown(400, 'easeOutBounce');
				$parentlink.addClass('active');
			}
		}
		else
		{
			document.location.href = $(this).find('a').attr('href');
		}
		return false;
	});
	$(document).click(function(){
		if ($('#nav-top li ul').is(':visible'))
		{
			$('#nav-top li a').removeClass('active');
			$('#nav-top li ul').slideUp(100, 'swing');
		}
	});
	/* End: Header-Right Navigation */

	/* Begin: Responsive Navigation */
	$('#nav-left .inline .fixed').html($('#nav-main').html()).css({ height: $(document).height() + 130 + 'px' });
	$('#nav-left ul.nav').css({ height: $(window).height() + 5 + 'px', overflow: 'auto' });
	var $navstatus = true;
	$('a.open-nav').click(function(){
		if (!$navstatus)
		{
			$(this).removeClass('open-nav-active');
			$navstatus = true;
			$('#wrap').animate({ marginLeft: '0' }, 500, 'easeOutBounce');
			$('#wrap .header, #wrap .footer').animate({ left: '0' }, 500, 'easeOutBounce');
		}
		else
		{
			$(this).addClass('open-nav-active');
			$navstatus = false;
			$('#wrap').animate({ marginLeft: '200px' }, 500, 'easeOutBounce');
			$('#wrap .header, #wrap .footer').animate({ left: '200px' }, 500, 'easeOutBounce');
		}
		return false;
	});
	$(window).resize(function() {
		if (!$navstatus)
		{
			$('a.open-nav').removeClass('open-nav-active');
			$('#nav-left ul.nav').css({ height: $(window).height() + 5 + 'px', overflow: 'auto' });
			if ($(document).width() > 780)
			{
				$navstatus = true;
				$('#wrap').animate({ marginLeft: '0' }, 500, 'easeOutBounce');
				$('#wrap .header, #wrap .footer').animate({ left: '0' }, 500, 'easeOutBounce');
			}
		}
	});
	/* End: Responsive Navigation */

	/* Begin: Sidebar Navigation */
	$('#nav-sidebar li').each(function(){
		if ($(this).children('ul').html())
		{
			var icon = 'down';
			if ($(this).attr('class') == 'active')
			{
				icon = 'up';
			}
			var $append = '<em class="icon-angle-' + icon + '"></em>';
			$(this).find('a:first-child').append($append);
		}
		$(this).find('li').find('em').remove();
	});

	$('#nav-sidebar li').click(function(){

		$redirect = false;

		/* Begin: Sidebar Auto Fixing */
		$('#wrap .sidebar').resize(function(){
			if ($(window).height() - 100 <= $('#wrap .sidebar ul.nav').height())
			{
				$('#wrap .sidebar').css({ position: 'relative' });
			}
			else
			{
				$('#wrap .sidebar').css({ position: 'fixed', height: '100%' });
			}
		});
		$(window).resize(function(){
			if ($(window).height() - 100 <= $('#wrap .sidebar ul.nav').height())
			{
				$('#wrap .sidebar').css({ position: 'relative' });
			}
			else
			{
				$('#wrap .sidebar').css({ position: 'fixed', height: '100%' });
			}
		});
		/* End: Sidebar Auto Fixing */

		if ($(this).find('span').is(':visible'))
		{
			var $subnav = $(this).children('ul');
			if ($subnav.html())
			{
				var $parent = $subnav.parent('li');
				var $arrow = $(this).find('em');
				if ($subnav.is(':visible'))
				{
					$subnav.slideUp(200, 'swing');
					$parent.removeClass('open');
					$arrow.addClass('icon-angle-down');
					$arrow.removeClass('icon-angle-up');
				}
				else
				{
					$subnav.slideDown(400, 'easeOutBounce');
					$parent.addClass('open');
					$arrow.addClass('icon-angle-up');
					$arrow.removeClass('icon-angle-down');
				}
			}
			else
			{
				$redirect = true;
			}
		}
		else
		{
			$redirect = true;
		}
		if ($redirect)
		{
			document.location.href = $(this).find('a').attr('href');
		}

		return false;
	});
	/* End: Sidebar Navigation */

	/* Begin: Tooltips */
	$('#wrap .header a, #wrap .content .content-top *').tooltip({ placement: 'right' });
	$('#wrap .sidebar *').tooltip({ placement: 'top' });
	$('#wrap .content .content-bottom a').tooltip();
	
    /* Begin: ScrollUp */
    $(document.body).append('<a href="#" class="scrollUp"><i class="icon-angle-up icon-2x"></i></a>');
    $(window).scroll(function(){
        if ($(this).scrollTop() > 100) {
            $('a.scrollUp').fadeIn();
        }
        else
        {
            $('a.scrollUp').fadeOut();
        }
    });
    $('a.scrollUp').click(function(){
        $("html, body").animate({ scrollTop: 0 }, 600);
        return false;
    });
    /* End: ScrollUp */

	/* Begin: Content Boxes */
	$('#wrap .middle .content .content-bottom .box .title .actions .item i.collapse-box-toggle').click(function(){
		var $icon = $(this);
		var $parentBox = $(this).parents('.box');
		var $block = $parentBox.find('.block');
		if ($block.is(':visible'))
		{
			$icon.removeClass('icon-minus');
			$icon.addClass('icon-plus');
			$parentBox.addClass('box-close');
			$block.slideUp('fast');
		}
		else
		{
			$icon.removeClass('icon-plus');
			$icon.addClass('icon-minus');
			$parentBox.removeClass('box-close');
			$block.slideDown('fast');
		}
		return false;
	});
	/* End: Content Boxes */

	/* Begin: Google PrettyPrint */
	window.prettyPrint && prettyPrint();
	/* End: Google PrettyPrint */

	/* Begin: Theme Roller */
	$('#theme-roller li a').click(function(){
		var $newTheme = $(this).attr('href').replace('#', '');
		themeRoller($newTheme);
		window.frames["menuFrame"].themeRoller($newTheme);
		window.frames["mainFrame"].themeRoller($newTheme);
		return false;
	});
	/* End: Theme Roller */

	/* Begin: Page Preloader */
	$window.load(function() {
		$('#preloader .inline').fadeOut();
		$('#preloader').delay(350).fadeOut('slow');
	});
	/* End: Page Preloader */	
	
}(window.jQuery);